﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BudgetMealsModels.Entities;
using System.Data.Entity;
using System.Web;

namespace BudgetMealsModels.Concrete
{
    public class EFDbContext : DbContext
    {
        public static EFDbContext Context { get { return HttpContext.Current.Items["EntityContext"] as EFDbContext; } }
        public DbSet<User> Users { get; set; }
        public DbSet<Recipe> Recipes { get; set; }
        public DbSet<Meal> Meals { get; set; }
        public DbSet<Comment> Comments { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Meal>().HasRequired<Recipe>(m => m.Appetizer).WithMany(r => r.Appetizers).WillCascadeOnDelete(false);
            modelBuilder.Entity<Meal>().HasRequired<Recipe>(m => m.MainCourse).WithMany(r => r.MainCourses).WillCascadeOnDelete(false);
            modelBuilder.Entity<Meal>().HasRequired<Recipe>(m => m.Dessert).WithMany(r => r.Desserts).WillCascadeOnDelete(false);
            modelBuilder.Entity<Recipe>().HasRequired<User>(r => r.User).WithMany(u => u.Recipes).WillCascadeOnDelete(false);
            modelBuilder.Entity<Meal>().HasRequired<User>(m => m.User).WithMany(u => u.Meals).WillCascadeOnDelete(false);
            //modelBuilder.Entity<User>().HasRequired<Cookbook>(m => m.Cookbook).WithRequiredPrincipal(c => c.User);
            modelBuilder.Entity<Recipe>().HasMany<Ingredient>(r => r.Ingredients).WithMany(i => i.Recipes);
            modelBuilder.Entity<Recipe>().HasMany<Image>(r => r.ImageGallery).WithOptional().WillCascadeOnDelete();
            modelBuilder.Entity<Recipe>().HasMany<Comment>(r => r.Comments).WithRequired(c => c.Recipe);
        }
    }
}
